package gov.pbc.nn.kjgl.archive.view;

import gov.pbc.nn.kjgl.archive.dao.ArchivesInputDao;
import gov.pbc.nn.kjgl.entity.Archives;
import gov.pbc.nn.kjgl.entity.ArchivesContent;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.core.Configure;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.uploader.DownloadFile;
import com.bstek.dorado.uploader.UploadFile;
import com.bstek.dorado.uploader.annotation.FileProvider;
import com.bstek.dorado.uploader.annotation.FileResolver;

@Component("archivesInputPR")
public class ArchivesInputPR {

	@Autowired
	private ArchivesInputDao archivesInputDao;

	@DataProvider
	public List<Archives> getArchives() {
		return archivesInputDao.getArchives();

	}

	@DataProvider
	public List<Archives> getArchivesByParentId(String parentId) {
		return archivesInputDao.getArchivesByParentId(parentId);
	}


	@DataProvider
	public void getArchiveContentByArchivesId(Page<ArchivesContent> page,Criteria criteria, Map<String, Object> parameter) {
		if (parameter != null) {
			archivesInputDao.getArchiveContentByArchivesId(page, criteria,(String) parameter.get("archivesId"));
		}

	}

	@SuppressWarnings("unused")
	@FileResolver
	@Transactional
	public String process2(UploadFile file, Map<String, Object> parameter) throws Exception {
		String name = (String) parameter.get("name");
		String archivesId = (String) parameter.get("archivesId");
		if (StringUtils.isEmpty(archivesId)) {
			throw new Exception("必须指定档案分类才能上传！");
		}
		String basePath = Configure.getString("ARCHIVE_FILE_UPLOAD_PATH");
		if (StringUtils.isEmpty(basePath)) {
			throw new IllegalArgumentException("必须指定档案文件上传的基本地址");
		}
		String returnValue = "";
		try {
//			if (archivesInputDao.check(name, archivesId)) {
//				String[] pathArr = null;
//				Archives ar = this.archivesInputDao.getArchivesById(archivesId);
//				if (StringUtils.isEmpty(ar.getParentId())) {
//					// 一级分类
//					pathArr = new String[1];
//					pathArr[0] = ar.getName();
//				} else {
//					pathArr = new String[2];
//					pathArr[1] = ar.getName();
//					ar = this.archivesInputDao.getArchivesById(ar.getParentId());
//					pathArr[0] = ar.getName();
//				}
				Path path = Paths.get(basePath);
				if (!Files.isDirectory(path)) {
					path = Files.createDirectories(path);
				}
				path = path.resolve(UUID.randomUUID().toString());
				File destFile = Files.createFile(path).toFile();
				archivesInputDao.saveArchivesContent(archivesId, file.getFileName(), path);
				file.transferTo(destFile);
				returnValue = "ok";
//			} else {
//				returnValue = "error";
//			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return returnValue;
	}

	@Expose
	@Transactional
	public void deleteArchivesContent(List<String> ids) throws IOException {
		for (String id : ids) {
			archivesInputDao.delArchivesContent(id);
		}
	}
	
	@Expose
	@Transactional
	public  void updateContent(List<String> ids,String arid) throws IOException {
		for (String id : ids) {
			archivesInputDao.updateContent(id,arid);
		}
	}
	
	@FileProvider
	public DownloadFile download(Map<String, String> parameter) throws IOException {
		String fileName = parameter.get("fileName");
		String path = parameter.get("path");
		InputStream stream = new FileInputStream(path);
		DownloadFile file = new DownloadFile(fileName, stream);
		return file;
	}

	// @Expose
	// @Transactional
	// public boolean checkArchivesContent(String archivesId) {
	// return archivesInputDao.checkArchivesContent(archivesId, type);
	// }

	@Expose
	@Transactional
	public boolean checkArchivesContent(Map<String, Object> parameter) {
		String archivesId = (String) parameter.get("archivesId");
		int type = (int) parameter.get("type");
		return archivesInputDao.checkArchivesContent(archivesId, type);
	}

}
